#include "caresize.h"

Caresize::Caresize()
{
    seamsAdded=0;
    seamsProcessed=0;
    fractionAdd=0;
    maxSeams=0;
    energy=NULL;
    m=NULL;
    seamMap=NULL;

}

Caresize::Caresize(QImage image)
{
    seamsAdded=0;
    seamsProcessed=0;
    energy=NULL;
    m=NULL;
    this->image=image;
    this->workGray=image;
    transformGray();
    seamMap=createMatrix(image.width(),image.height());
    originalHeight=image.height();
    originalWidth=image.width();
}

void Caresize::horizontalInit()
{
    fractionAdd=image.height()/2;
    maxSeams=image.height()-5;
    minSeams=5;
}

void Caresize::verticalInit()
{
    fractionAdd=image.width()/2;
    maxSeams=image.width()-5;
    minSeams=5;
}


Caresize::~Caresize()
{
    destroyMatrix(energy,workGray.width());
    destroyMatrix(seamMap,image.width());
}

int Caresize::maxResizeH()
{
    return seamsAdded+originalHeight;
}

int Caresize::maxResizeV()
{
    return seamsAdded+originalWidth;
}

int Caresize::minResizeH()
{
    return originalHeight-seamsProcessed;
}

int Caresize::minResizeV()
{
    return originalWidth-seamsProcessed;
}

int ** Caresize::horizontalM()
{
    int width=workGray.width()+1;
    int height=workGray.height();

    int ** m=new int*[width];
        for(int i=0;i<width;i++)
            m[i]=new int[height];

    //Initialize zeros at the beginning
    for(int i=0;i<height;i++)
        m[0][i]=0;
    //Calculate values of the matrix
    for(int i=1;i<width;i++)
        for(int j=0;j<height;j++)
        {
            int min=m[i-1][j];
            if(j-1>=0)
                if(min>m[i-1][j-1])
                    min=m[i-1][j-1];
            if(j+1<height)
                if(min>m[i-1][j+1])
                    min=m[i-1][j+1];
            m[i][j]=energy[i-1][j]+min;
        }

    return m;
}

int * Caresize::horizontalBacktrack()
{
    int width=workGray.width()+1;
    int height=workGray.height();
    int *seam=new int[workGray.width()];
    int min=0;

    for(int j=0;j<height;j++)
    {
        if(m[width-1][min]>m[width-1][j])
            min=j;

    }
    int j=min;
    for(int i=width-1;i>0;i--)
    {
            seam[i-1]=j;
            int compare=m[i][j]-energy[i-1][j];
            if(compare==m[i-1][j])
                continue;
            else if(j-1>=0)
            {
                    if(compare==m[i-1][j-1])
                        j=j-1;
                    else j=j+1;
            }
    }
    return seam;
}

int * Caresize::horizontalSeam()
{
    m=horizontalM();
    int *seam= horizontalBacktrack();
    destroyMatrix(m,workGray.width()+1);
    return seam;
}

void Caresize::horizontalProcess(){
    int * seam=horizontalSeam();
    updateHSeamMap(seam);
    QImage newgray(workGray.width(),workGray.height()-1,workGray.format());
    if(workGray.depth()<=8)
    {
        for(int i=0;i<workGray.width();i++)
            for(int j=0;j<workGray.height()-1;j++)
            {
               if(j<seam[i])
               {
                   newgray.setPixel(i,j,workGray.pixelIndex(i,j));
               }else
               {
                    newgray.setPixel(i,j,workGray.pixelIndex(i,j+1));
               }
            }
    }else
    {
        for(int i=0;i<workGray.width();i++)
            for(int j=0;j<workGray.height()-1;j++)
            {
                if(j<seam[i])
                    {
                        newgray.setPixel(i,j,workGray.pixel(i,j));
                    }else
                    {
                         newgray.setPixel(i,j,workGray.pixel(i,j+1));
                    }
            }
    }

    workGray=newgray;
    horizontalUpdateEnergy(seam);
    delete [] seam;
}
void Caresize::horizontalProcessN(int num)
{
    if(energy==NULL)
    {
        energy=createMatrix(workGray.width(),workGray.height());
        sobel();
    }
    if(num+seamsProcessed>maxSeams)
        num=maxSeams-seamsProcessed;
    for(int i=0;i<num;i++)
    {
       horizontalProcess();
    }
}

void Caresize::horizontalAddN(int num)
{
    num+=seamsAdded;
    if(num > seamsProcessed)
    {   if(seamsProcessed<maxSeams)
        {
            int toprocess=num-seamsProcessed;
            if(toprocess+seamsProcessed>=maxSeams)
                toprocess=maxSeams-seamsProcessed;
            horizontalProcessN(toprocess);
        }
    }
        for(int i=seamsAdded;i<num;i++)
        {
            int *seam=getHSeam(((i)%fractionAdd)+1);
            addHSeamMap(seam);
            addHSeamImage(seam);
            delete [] seam;
        }
}

QImage Caresize::resizeImageH(int newheight)
{
    if(newheight<5)
        newheight=5;
    int nums=abs(newheight-image.height());
    int originalheight=image.height()-seamsAdded;
    if(newheight==image.height() || nums==0)
        return image;
   // cout << "Original Image. width=" << image.width() << " height=" << image.height() << endl;
    //cout << "Destination Image. width=" << image.width() << " height=" << newheight << endl;
    if(newheight<=originalheight)
    {
        int toprocess=originalheight-newheight-seamsProcessed;
        if(toprocess>0){
            horizontalProcessN(toprocess);
        }
    }else
    {
        int toprocess= newheight-image.height();
        if(toprocess>0)
        {
            if(toprocess>maxSeams)
                toprocess=maxSeams-seamsProcessed;
            horizontalProcessN(toprocess);
        }
    }


    if(newheight>image.height())
    {
        nums+=seamsAdded;
        for(int i=seamsAdded;i<nums;i++)
        {
            int *seam=getHSeam(((i)%fractionAdd)+1);
            addHSeamMap(seam);
            addHSeamImage(seam);
            delete [] seam;
        }
        /*
        if(image.height()==newheight)
            cout << "Finished resizing properly" << endl << endl;
        else
            cout << "FAILED" << endl << endl;
        */
        return image;
    }
    QImage newimage(image.width(),newheight,image.format());
    nums=image.height()-seamsAdded-newheight;
    if(nums<0) nums--;
    int newx=0;
    for(int x=0; x<image.width() ;x++){
        int newy=0;
        for(int y=0;y<image.height();y++)
        {
            if(seamMap[x][y]==0 || seamMap[x][y]>nums)
            {
                newimage.setPixel(newx,newy, image.pixel(x,y));
                newy++;
                //count++;
            }
        }
        newx++;
    }
    /*
    if(newimage.height()==newheight)
        cout << "Finished resizing properly" << endl << endl;
    else
        cout << "FAILED" << endl << endl;
    */
    return newimage;
}

void Caresize::updateHSeamMap(int *seam){
    seamsProcessed++;
    for(int i=0;i<image.width();i++)
    {
        int accum=0;
        for(int j=0; j<image.height();j++)
        {
            if(seamMap[i][j]==0)
            {
                if(accum==seam[i])
                {
                    seamMap[i][j]=seamsProcessed;
                    break;
                }
                else
                    accum++;
            }
        }
    }
    //cout << "Seams Processed: "<< seamsProcessed << endl;
}

void Caresize::horizontalUpdateEnergy(int *seam)
{
    //New matrix with one less height
    int ** newenergy=createMatrix(workGray.width(), workGray.height());

    //Copy values of old energy without the seam to be destroyed
    for(int i=0;i<workGray.width();i++)
        for(int j=0;j<workGray.height();j++)
        {
           if(j<seam[i] && newenergy[i][j]!=-1)
           newenergy[i][j]=energy[i][j];
           else if(j==seam[i])
           {
               for(int x=-1;x<2;x++)
                   for(int y=-1;y<1;y++)
                   { //cout << x+i << ", " << j+y << ". ";
                       if(x+i>=0 && x+i<workGray.width() && j+y>=0)
                       {
                            newenergy[x+i][j+y]=-1;
                            //cout << x+i << ", " << j+y << endl;
                        }
                   }
           }else if(newenergy[i][j]!=-1)
            newenergy[i][j]=energy[i][j+1];
       }
    destroyMatrix(energy,workGray.width());
    energy=newenergy;

    //Fix with little convolution
    for(int i=0;i<workGray.width();i++)
    {

        int sobely[9]={-1,-2,-1,0,0,0,1,2,1}; //inverted
        int sobelx[9]={-1,0,1,-2,0,2,-1,0,1}; //inverted
        for(int h=-1;h<2;h++)
        {
            int dx=0;
            int dy=0;
            int dx2=0;
            int dy2=0;
            if(h+i>=0 && h+i<workGray.width() && seam[i]-1>=0 && seam[i]-1<workGray.height() && energy[h+i][seam[i]-1]==-1)
            {
                for(int y=0;y<3;y++)
                    for(int x=0;x<3;x++)
                    {

                        int ximage=i+x-1+h;
                        int yimage=seam[i]-1+y-1;
                        if(ximage>=0 && ximage<workGray.width() && yimage>=0 && yimage<workGray.height())
                        {
                            dx+=sobelx[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                            dy+=sobely[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                        }
                    }
                energy[i+h][seam[i]-1]=sqrt(dx*dx+dy*dy);
            }
            if(h+i>=0 && h+i<workGray.width() && seam[i]>=0 && seam[i]<workGray.height() && energy[h+i][seam[i]]==-1)
            {
                for(int y=0;y<3;y++)
                    for(int x=0;x<3;x++)
                    {
                        int ximage=i+x-1+h;
                        int yimage=seam[i]+y-1;
                        if(ximage>=0 && ximage<workGray.width() && yimage>=0 && yimage<workGray.height())
                        {
                            dx2+=sobelx[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                            dy2+=sobely[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                        }
                    }
                energy[i+h][seam[i]]=sqrt(dx2*dx2+dy2*dy2);
            }
        }

    }
    //Done updating the energy matrix after removing horizontal Seam
}

void Caresize::addHSeamMap(int *seam){
    seamsAdded++;
    int ** newMap=createMatrix(image.width(),image.height()+1);
    for(int i=0;i<image.width();i++)
        for(int j=0;j<image.height();j++)
        {
            if(j<seam[i])
                newMap[i][j]=seamMap[i][j];
            else if(j==seam[i])
            {
                newMap[i][j]=seamMap[i][j];
                newMap[i][j+1]=-seamsAdded;
            }else
                newMap[i][j+1]=seamMap[i][j];
        }
    destroyMatrix(seamMap,image.width());
    seamMap=newMap;
}

void Caresize::addHSeamImage(int *seam){
    QImage newimage(image.width(),image.height()+1,image.format());
    for(int i=0;i<image.width();i++)
        for(int j=0;j<image.height();j++)
        {
            if(j<seam[i])
                newimage.setPixel(i,j,image.pixel(i,j));
            else if(j==seam[i])
            {
                newimage.setPixel(i,j,image.pixel(i,j));
                if(j+1<image.height())
                {
                    QRgb value1=image.pixel(i,j);
                    QRgb value2=image.pixel(i,j+1);
                    int red=(qRed(value1)+qRed(value2))/2;
                    int green=(qGreen(value1)+qGreen(value2))/2;
                    int blue=(qBlue(value1)+qBlue(value2))/2;
                    newimage.setPixel(i,j+1,qRgb(red,green,blue));
                }else
                    newimage.setPixel(i,j+1,image.pixel(i,j));
            }else
                newimage.setPixel(i,j+1,image.pixel(i,j));
        }
    image=newimage;
    //cout << "Seams Added: "<< seamsAdded << endl;
}

int *Caresize::getHSeam(int num){
    int * seam=new int[image.width()];
    for(int x=0;x<image.width();x++)
        for(int y=0;y<image.height();y++)
            if(seamMap[x][y]==num)
            {
                seam[x]=y;
                break;
            }
    return seam;
}


void Caresize::horizontalPaint(int* seam){
    if(image.depth()<=8)
    {
        for(int i=0;i<image.width();i++)
               image.setPixel(i,seam[i],255);
    }else
    {
        for(int i=0;i<image.width();i++)
               image.setPixel(i,seam[i],qRgb(255,255,255));
    }
}

//VERTICALLLL

int ** Caresize::verticalM()
{
    int width=workGray.width();
    int height=workGray.height()+1;

    int ** m=new int*[width];
        for(int i=0;i<width;i++)
            m[i]=new int[height];

    //Initialize zeros at the beginning
    for(int i=0;i<width;i++)
        m[i][0]=0;
    //Calculate values of the matrix
    for(int j=1;j<height;j++)
        for(int i=0;i<width;i++)
        {
            int min=m[i][j-1];
            if(i-1>=0)
                if(min>m[i-1][j-1])
                    min=m[i-1][j-1];
            if(i+1<width)
                if(min>m[i+1][j-1])
                    min=m[i+1][j-1];
            m[i][j]=energy[i][j-1]+min;
        }

    return m;
}

int * Caresize::verticalBacktrack()
{
    int width=workGray.width();
    int height=workGray.height()+1;
    int *seam=new int[workGray.height()];
    int min=0;

    for(int i=0;i<width;i++)
    {
        if(m[min][height-1]>m[min][height-1])
            min=i;

    }
    int i=min;
    for(int j=height-1;j>0;j--)
    {
            seam[j-1]=i;
            int compare=m[i][j]-energy[i][j-1];
            if(compare==m[i][j-1])
                continue;
            else if(i-1>=0)
            {
                    if(compare==m[i-1][j-1])
                        i=i-1;
                    else i=i+1;
            }
    }
    return seam;
}

int * Caresize::verticalSeam()
{
    m=verticalM();
    int *seam= verticalBacktrack();
    destroyMatrix(m,workGray.width());
    return seam;
}

void Caresize::verticalProcess(){
    int * seam=verticalSeam();
    updateVSeamMap(seam);
    QImage newgray(workGray.width()-1,workGray.height(),workGray.format());
    if(workGray.depth()<=8)
    {
        for(int j=0;j<workGray.height();j++)
            for(int i=0;i<workGray.width()-1;i++)
            {
               if(i<seam[j])
               {
                   newgray.setPixel(i,j,workGray.pixelIndex(i,j));
               }else
               {
                    newgray.setPixel(i,j,workGray.pixelIndex(i+1,j));
               }
            }
    }else
    {
        for(int j=0;j<workGray.height();j++)
            for(int i=0;i<workGray.width()-1;i++)
            {
                if(i<seam[j])
                    {
                        newgray.setPixel(i,j,workGray.pixel(i,j));
                    }else
                    {
                         newgray.setPixel(i,j,workGray.pixel(i+1,j));
                    }
            }
    }

    workGray=newgray;
    verticalUpdateEnergy(seam);
    delete [] seam;
}

void Caresize::verticalAddN(int num)
{
        num+=seamsAdded;
        if(num > seamsProcessed)
        {   if(seamsProcessed<maxSeams)
            {
                int toprocess=num-seamsProcessed;
                if(toprocess+seamsProcessed>=maxSeams)
                    toprocess=maxSeams-seamsProcessed;
                verticalProcessN(toprocess);
            }
        }
        for(int i=seamsAdded;i<num;i++)
        {
            int *seam=getVSeam(((i)%fractionAdd)+1);
            addVSeamMap(seam);
            addVSeamImage(seam);
            delete [] seam;
        }
}
void Caresize::verticalProcessN(int num)
{
    if(energy==NULL)
    {
        energy=createMatrix(workGray.width(),workGray.height());
        sobel();
    }
    if(num+seamsProcessed>maxSeams)
        num=maxSeams-seamsProcessed;

    for(int i=0;i<num;i++)
    {
       verticalProcess();
    }
}
//KEEP GOING FROM HERE!
QImage Caresize::resizeImageV(int newwidth)
{
    //assertSeamMap();
    if(newwidth<5)
        newwidth=5;
    int nums=abs(newwidth-image.width());
    int originalwidth=image.width()-seamsAdded;
    if(newwidth==image.width() || nums==0)
        return image;
    //cout << "Original Image. width=" << image.width() << " height=" << image.height() << endl;
    //cout << "Destination Image. width=" << newwidth << " height=" << image.height() << endl;
    if(newwidth<=originalwidth)
    {
        int toprocess=originalwidth-newwidth-seamsProcessed;
        if(toprocess>0){
            verticalProcessN(toprocess);
        }
    }else
    {
        int toprocess= newwidth-image.width();
        if(toprocess>0)
        {
            if(toprocess>maxSeams)
                toprocess=maxSeams-seamsProcessed;
            verticalProcessN(toprocess);
        }
    }
    if(newwidth>image.width())
    {
        nums+=seamsAdded;
        for(int i=seamsAdded;i<nums;i++)
        {
            int *seam=getVSeam(((i)%fractionAdd)+1);
            addVSeamMap(seam);
            addVSeamImage(seam);
            delete [] seam;
        }
        /*
        if(image.width()==newwidth)
            cout << "Finished resizing properly" << endl << endl;
        else
            cout << "FAILED" << endl << endl;
        */
        return image;
    }
    QImage newimage(newwidth,image.height(),image.format());
    nums=image.width()-seamsAdded-newwidth;
    if(nums<0) nums--;
    int newy=0;
    for(int y=0; y<image.height() ;y++){
        int newx=0;
        for(int x=0;x<image.width();x++)
        {
            if(seamMap[x][y]==0 || seamMap[x][y]>nums)
            {
                newimage.setPixel(newx,newy, image.pixel(x,y));
                newx++;
                //count++;
            }
        }
        newy++;
    }
    /*
    if(newimage.width()==newwidth)
        cout << "Finished resizing properly" << endl << endl;
    else
        cout << "FAILED" << endl << endl;
    */
    return newimage;
}

void Caresize::updateVSeamMap(int *seam){
    seamsProcessed++;
    for(int j=0;j<image.height();j++)
    {
        int accum=0;
        for(int i=0; i<image.width();i++)
        {
            if(seamMap[i][j]==0)
            {
                if(accum==seam[j])
                {
                    seamMap[i][j]=seamsProcessed;
                    break;
                }
                else
                    accum++;
            }
        }
    }/*
    cout << "Seams Processed: "<< seamsProcessed << endl;
    */
}

void Caresize::verticalUpdateEnergy(int *seam)
{
    //New matrix with one less height
    int ** newenergy=createMatrix(workGray.width(), workGray.height());

    //Copy values of old energy without the seam to be destroyed
    for(int j=0;j<workGray.height();j++)
        for(int i=0;i<workGray.width();i++)
        {
           if(i<seam[j] && newenergy[i][j]!=-1)
           newenergy[i][j]=energy[i][j];
           else if(i==seam[j])
           {
               for(int y=-1;y<2;y++)
                   for(int x=-1;x<1;x++)
                   { //cout << x+i << ", " << j+y << ". ";
                       if(y+j>=0 && y+j<workGray.height() && i+x>=0)
                       {
                            newenergy[x+i][j+y]=-1;
                            //cout << x+i << ", " << j+y << endl;
                        }
                   }
           }else if(newenergy[i][j]!=-1)
            newenergy[i][j]=energy[i+1][j];
       }
    destroyMatrix(energy,workGray.width());
    energy=newenergy;

    //Fix with little convolution
    for(int j=0;j<workGray.height();j++)
    {

        int sobely[9]={-1,-2,-1,0,0,0,1,2,1}; //inverted
        int sobelx[9]={-1,0,1,-2,0,2,-1,0,1}; //inverted
        for(int h=-1;h<2;h++)
        {
            int dx=0;
            int dy=0;
            int dx2=0;
            int dy2=0;
            if(h+j>=0 && h+j<workGray.height() && seam[j]-1>=0 && seam[j]-1<workGray.width() && energy[seam[j]-1][h+j]==-1)
            {
                for(int x=0;x<3;x++)
                    for(int y=0;y<3;y++)
                    {

                        int ximage=seam[j]-1+x-1;
                        int yimage=j+y-1+h;
                        if(ximage>=0 && ximage<workGray.width() && yimage>=0 && yimage<workGray.height())
                        {
                            dx+=sobelx[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                            dy+=sobely[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                        }
                    }
                energy[seam[j]-1][j+h]=sqrt(dx*dx+dy*dy);
            }
            if(h+j>=0 && h+j<workGray.height() && seam[j]>=0 && seam[j]<workGray.width() && energy[seam[j]][h+j]==-1)
            {
                for(int x=0;x<3;x++)
                    for(int y=0;y<3;y++)
                    {
                        int ximage=seam[j]+x-1;
                        int yimage=j+y-1+h;
                        if(ximage>=0 && ximage<workGray.width() && yimage>=0 && yimage<workGray.height())
                        {
                            dx2+=sobelx[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                            dy2+=sobely[x+3*y]*qBlue(workGray.pixel(ximage,yimage));
                        }
                    }
                energy[seam[j]][j+h]=sqrt(dx2*dx2+dy2*dy2);
            }
        }

    }
    //Done updating the energy matrix after removing vertical Seam
}

void Caresize::addVSeamMap(int *seam){
    seamsAdded++;
    int ** newMap=createMatrix(image.width()+1,image.height());
    for(int j=0;j<image.height();j++)
        for(int i=0;i<image.width();i++)
        {
            if(i<seam[j])
                newMap[i][j]=seamMap[i][j];
            else if(i==seam[j])
            {
                newMap[i][j]=seamMap[i][j];
                newMap[i+1][j]=-seamsAdded;
            }else
                newMap[i+1][j]=seamMap[i][j];
        }
    destroyMatrix(seamMap,image.width());
    seamMap=newMap;
}

void Caresize::addVSeamImage(int *seam){
    QImage newimage(image.width()+1,image.height(),image.format());
    for(int j=0;j<image.height();j++)
        for(int i=0;i<image.width();i++)
        {
            if(i<seam[j])
                newimage.setPixel(i,j,image.pixel(i,j));
            else if(i==seam[j])
            {
                newimage.setPixel(i,j,image.pixel(i,j));
                if(i+1<image.width())
                {
                    QRgb value1=image.pixel(i,j);
                    QRgb value2=image.pixel(i+1,j);
                    int red=(qRed(value1)+qRed(value2))/2;
                    int green=(qGreen(value1)+qGreen(value2))/2;
                    int blue=(qBlue(value1)+qBlue(value2))/2;
                    newimage.setPixel(i+1,j,qRgb(red,green,blue));
                }else
                    newimage.setPixel(i+1,j,image.pixel(i,j));
            }else
                newimage.setPixel(i+1,j,image.pixel(i,j));
        }
    image=newimage;
    //cout << "Seams Added: "<< seamsAdded << endl;
}

int *Caresize::getVSeam(int num){
    int * seam=new int[image.height()];
    for(int y=0;y<image.height();y++)
        for(int x=0;x<image.width();x++)
            if(seamMap[x][y]==num)
            {
                seam[y]=x;
                break;
            }
    return seam;
}


void Caresize::verticalPaint(int* seam){
    if(image.depth()<=8)
    {
        for(int i=0;i<image.width();i++)
               image.setPixel(i,seam[i],255);
    }else
    {
        for(int i=0;i<image.width();i++)
               image.setPixel(i,seam[i],qRgb(255,255,255));
    }
}
int ** Caresize::createMatrix(int width, int height)
{
    int ** a=new int*[width];
        for(int i=0;i<width;i++)
            a[i]=new int[height];
        for(int i=0;i<width;i++)
            for(int j=0;j<height;j++)
                a[i][j]=0;
    return a;
}

void Caresize::destroyMatrix(int** a, int width)
{
    for (int i=0; i<width; i++)
        delete [] a[i];
    delete [] a;
}


void Caresize::assertEnergy()
{
    cout << "Assert" << endl;
    int **energy2=energy;
    sobel();
    if(energy2==energy)
        cout << "They are the same... ";
    for(int i=0;i<workGray.width();i++)
        for(int j=0;j<workGray.height();j++)
        {

            if(energy2[i][j]!=energy[i][j])
            {
                cout << "Assertion failed with " << i << "," << j << endl;
            }
        }
    cout << "End Assert" << endl;
    destroyMatrix(energy2,workGray.width());
}
void Caresize::assertSeamMap()
{
    cout << "Assert" << endl;


    for(int j=0;j<image.height();j++)
    {
        int count=0;
        for(int i=0;i<image.width();i++)
        {
            if(seamMap[i][j]!=0)
                count++;
        }
        if(count!=seamsProcessed+seamsAdded)
        {
            cout << "They are different= "<< count << ", " << seamsProcessed << ", " << seamsAdded << " = " << seamsProcessed+seamsAdded << endl;
        }
    }
    cout << "End Assert" << endl;
}

void Caresize::sobel(){
    SobelFilter sobel;
    int *output=sobel.intelSobel(&workGray);
    energy=createMatrix(workGray.width(),workGray.height());
    for(int i=0;i<workGray.width();i++)
        for(int j=0;j<workGray.height();j++)
            energy[i][j]=output[i+workGray.width()*j];
    delete [] output;
}

void Caresize::transformGray()
{
    for(int i=0;i<image.width();i++)
        for(int j=0;j<image.height();j++)
        {
            QRgb rgb=image.pixel(i,j);
            int v=0.3*qRed(rgb)+0.59*qGreen(rgb)+0.11*qBlue(rgb);
            workGray.setPixel(i,j,qRgb(v,v,v));
        }
}

QImage Caresize::getImage()
{
    return image;
}

